;
(function () {
    Array.prototype.max = function () {
        var max = this[0];
        var len = this.length;
        for (var i = 1; i < len; i++) {
            if (this[i] > max) {
                max = this[i];
            }
        }
        return max;
    }
    $('#addModal,#updateModal').on('hidden.bs.modal', function (e) {
        $(".public_form")[0].reset();
        $(".public_form").data('bootstrapValidator').destroy();
        $('.public_form').data('bootstrapValidator', null);
        validator();
    })

    // 日期插件加载
    $('.form_date').datetimepicker({
        language: 'zh-CN',
        weekStart: 1,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0
    }).on('hide', function (e) {
        $('.public_form').data('bootstrapValidator')
            .updateStatus($(this).attr("name"), 'NOT_VALIDATED', null)
            .validateField($(this).attr("name"));
        $('.update_form').data('bootstrapValidator')
            .updateStatus($(this).attr("name"), 'NOT_VALIDATED', null)
            .validateField($(this).attr("name"));
    });

    function year_select(val) {
        var date = new Date();
        var year = date.getFullYear();
        for (var i = 0; i < val; i++) {
            $(".year_select").append("<option value='" + year + "'>" + year + "</option>");
            year--;
        }
    }

    function month_select() {
        for (var i = 1; i <= 12; i++) {
            if (i > 9) {
                $(".month_select").append("<option value='" + i + "'>" + i + "</option>");
            } else {
                $(".month_select").append("<option value='0" + i + "'>0" + i + "</option>");
            }
        }
    }
    year_select(30);
    month_select();
    //开通日期改变事件
    $('#addktrq')
        .on('changeDate', function (ev) {
            var mydate = new Date(ev.date.valueOf());
            $('#addjfrq').datetimepicker('setStartDate', time(mydate));
            $('#addjsrq').datetimepicker('setStartDate', time(mydate));
        });
    $('#addjfrq')
        .on('changeDate', function (ev) {
            var mydate = new Date(ev.date.valueOf());
            $('#addjsrq').datetimepicker('setStartDate', time(mydate));
        });
    $('#addtz').bind('input propertychange', function () {
        if ($(this).val() != "") {
            $("#addtzsj").attr("disabled", false);
        } else {
            $("#addtzsj").attr("disabled", true);
            $("#addtzsj").datetimepicker('setEndDate', time(mydate));
        }
    });
    $('#updatektrq')
        .on('changeDate', function (ev) {
            var mydate = new Date(ev.date.valueOf());
            $('#updatejfrq').datetimepicker('setStartDate', time(mydate));
            $('#updatejsrq').datetimepicker('setStartDate', time(mydate));
        });
    $('#updatejfrq')
        .on('changeDate', function (ev) {
            var mydate = new Date(ev.date.valueOf());
            $('#updatejsrq').datetimepicker('setStartDate', time(mydate));
        });
    $('#updatetz').bind('input propertychange', function () {
        if ($(this).val() != "") {
            $("#updatetzsj").attr("disabled", false);
        } else {
            $("#updatetzsj").attr("disabled", true);
            $("#updatetzsj").datetimepicker('setEndDate', time(mydate));
        }
    });
    // 将时间转换成yyyymmdd格式
    function time(date) {
        var mydate = new Date(date);
        var str = "" + mydate.getFullYear();
        var mm = mydate.getMonth() + 1
        if (mydate.getMonth() > 9) {
            str += "-" + mm;
        } else {
            str += "-0" + mm;
        }
        if (mydate.getDate() > 9) {
            str += "-" + mydate.getDate();
        } else {
            str += "-0" + mydate.getDate();
        }
        return str;
    }

    //查询下拉框判断
    $(".father_select").on("change", function () {
        // 如果选择“请选择选项”就把月下拉框禁用掉
        if ($(this).val() == "") {
            $(".son_select").attr("disabled", true);
            $(".son_select").val("");
        } else {
            $(".son_select").attr("disabled", false);
        }
    })
    //点击认证可选认证日期
    $("#updateyesrz,#updatenorz").on("click", function () {
        if ($(this).val() == "是") {
            $("#updaterzsj").attr("disabled", false);
        } else {
            $("#updaterzsj").attr("disabled", true);
            $("#updaterzsj").val("")
            $('.public_form').data('bootstrapValidator').enableFieldValidators('rzsj', true);
        }
    })
    $("#addyesrz,#addnorz").on("click", function () {
        if ($(this).val() == "是") {
            $("#addrzsj").attr("disabled", false);
        } else {
            $("#addrzsj").attr("disabled", true);
            $("#addrzsj").val("")
            $('.public_form').data('bootstrapValidator').enableFieldValidators('rzsj', true);
        }
    });
    // 计算总额
    $(".table-body .count").each(function () {
        var id = $(this).attr("id");
        var num = 0;
        $("." + id).each(function () {
            num = (num*100+$(this).html()*100)/100;
        })
        $(this).html(num.toFixed(2))
    })
    setTimeout(function () {
        $('#collapseExample').collapse('show')
    }, 200)
})()